package com.supplychainapi.config;


import com.supplychainapi.utils.DateUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.web.bind.annotation.RequestMethod;
import springfox.documentation.builders.*;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.Date;

import static com.google.common.collect.Lists.newArrayList;


@Profile({"local", "dev", "test"})//在开发和测试开启
@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket createPhoneRestApi()
    {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiPhoneInfo())
                .groupName("supplychain-phone-api")
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.supplychainapi.controller"))
                .paths(PathSelectors.regex("/phone.*"))
                .build()
                .useDefaultResponseMessages(false)
                .globalResponseMessage(RequestMethod.POST,
                        newArrayList(
                                new ResponseMessageBuilder()
                                        .code(400001)
                                        .message("token错误")
                                        .responseModel(new ModelRef("Error"))
                                        .build(),
                                new ResponseMessageBuilder()
                                        .code(400002)
                                        .message("返回消息格式错误")
                                        .responseModel(new ModelRef("Error"))
                                        .build(),
                                new ResponseMessageBuilder()
                                        .code(400003)
                                        .message("签名算法版本必填")
                                        .responseModel(new ModelRef("Error"))
                                        .build(),
                                new ResponseMessageBuilder()
                                        .code(400004)
                                        .message("签名算法版本格式错误")
                                        .responseModel(new ModelRef("Error"))
                                        .build(),
                                new ResponseMessageBuilder()
                                        .code(400005)
                                        .message("签名必填")
                                        .responseModel(new ModelRef("Error"))
                                        .build(),
                                new ResponseMessageBuilder()
                                        .code(400006)
                                        .message("唯一随机数必填")
                                        .responseModel(new ModelRef("Error"))
                                        .build(),
                                new ResponseMessageBuilder()
                                        .code(400007)
                                        .message("请求的时间戳必填")
                                        .responseModel(new ModelRef("Error"))
                                        .build(),
                                new ResponseMessageBuilder()
                                        .code(400008)
                                        .message("签名验证错误")
                                        .responseModel(new ModelRef("Error"))
                                        .build()
                        ))
                .globalOperationParameters(
                        newArrayList(
                                new ParameterBuilder()
                                        .name("Token")
                                        .description("token签名")
                                        .modelRef(new ModelRef("string"))
                                        .parameterType("header")
                                        .defaultValue("swagger2")
                                        .build(),
                                new ParameterBuilder()
                                        .name("Format")
                                        .description("返回值类型,目前只支持JSON,默认JSON")
                                        .modelRef(new ModelRef("string"))
                                        .parameterType("header")
                                        .defaultValue("JSON")
                                        .required(true)
                                        .build(),
                                new ParameterBuilder()
                                        .name("SignatureVersion")
                                        .description("签名算法版本，目前版本是1.0，取值：1.0")
                                        .modelRef(new ModelRef("string"))
                                        .parameterType("header")
                                        .defaultValue("1.0")
                                        .required(true)
                                        .build(),
                                new ParameterBuilder()
                                        .name("Source")
                                        .description("来源;Android;IOS")
                                        .modelRef(new ModelRef("string"))
                                        .parameterType("header")
                                        .defaultValue("Android")
                                        .required(true)
                                        .build(),
                                new ParameterBuilder()
                                        .name("Signature")
                                        .description("签名结果串，关于签名的计算方法")
                                        .modelRef(new ModelRef("string"))
                                        .parameterType("header")
                                        .required(true)
                                        .build(),
                                new ParameterBuilder()
                                        .name("SignatureNonce")
                                        .description("唯一随机数，用于防止网络重放攻击。用户在不同请求间要使用不同的随机数值;8位随机数字")
                                        .modelRef(new ModelRef("string"))
                                        .parameterType("header")
                                        .defaultValue("88888888")
                                        .required(true)
                                        .build(),
                                new ParameterBuilder()
                                        .name("Timestamp")
                                        .description("请求的时间戳。格式为： YYYY-MM-DD hh:mm:ss 例如，2013-01-10 12:00:00（为北京时间2013年1月10日20点0分0秒）")
                                        .modelRef(new ModelRef("string"))
                                        .parameterType("header")
                                        .defaultValue(DateUtils.formatDateToString(new Date(), DateUtils.DEFAULT_DATE_FORMAT2))
                                        .required(true)
                                        .build(),
                                new ParameterBuilder()
                                        .name("version")
                                        .description("版本号默认v1 该参数不参与验签")
                                        .modelRef(new ModelRef("string"))
                                        .parameterType("path")
                                        .defaultValue("v1")
                                        .required(true)
                                        .build()
                        ));
    }

    private ApiInfo apiPhoneInfo()
    {
        return new ApiInfoBuilder()
                .title("移动端接口")
                .description("移动端接口")
                .version("1.0")
                .build();
    }


}
